package id.co.rho.scoring.cid.client.mvc.upload;

import id.co.rho.scoring.cid.client.mvc.AppView;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;

public class UploadModelUIWeb implements UploadModelUI {

	private ContentPanel localPanel;
	private FieldSet fieldSet;
	private ContentPanel centerPanel;
	private FormPanel panel;
	private Button btnReset;
	private Button btnSubmit;
	private TextField<String> fieldName;
	private TextField<String> fieldType;

	@Override
	public void initialize() {
		localPanel = new ContentPanel();
		FormLayout formlayout = new FormLayout();
		formlayout.setLabelAlign(FormPanel.LabelAlign.LEFT);

		fieldSet = new FieldSet();
		fieldSet.setHeading("Upload Model");
		fieldSet.setLayout(formlayout);

		panel = new FormPanel();
		panel.setHeading("Model File Upload");
		panel.setFrame(true);
		panel.setAction(GWT.getModuleBaseURL() + "uploadModelServlet"
				+ "?pType=uploadModel");
		panel.setEncoding(Encoding.MULTIPART);
		panel.setMethod(Method.POST);
		panel.setButtonAlign(HorizontalAlignment.LEFT);
		panel.setWidth(350);

		fieldName = new TextField<String>();
		fieldName.setName("fieldName");
		fieldName.setEmptyText("Input model name ..."); 
		fieldName.setHideLabel(false);
		fieldName.setFieldLabel("Model Name");
		fieldName.setStyleAttribute("padding-bottom", "5px");
		fieldName.setAllowBlank(false);
		panel.add(fieldName);

		fieldType = new TextField<String>();
		fieldType.setName("fieldType");
		fieldType.setEmptyText("Input model type ..."); 
		fieldType.setHideLabel(false);
		fieldType.setFieldLabel("Model Type");
		fieldType.setStyleAttribute("padding-bottom", "5px");
		fieldType.setAllowBlank(true);
		panel.add(fieldType);
		
		FileUploadField file = new FileUploadField();
		file.setAllowBlank(false);
		file.setName("uploadedfile");
		file.setFieldLabel("Model File");
		panel.add(file);

		btnReset = new Button("Reset");
		btnReset.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				panel.reset();
			}
		});

		btnSubmit = new Button("Submit");
		btnSubmit.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				if (!panel.isValid()) {
					return;
				}
				panel.submit();
				panel.reset();
				MessageBox.info("Action", "You file was uploaded", null);
			}
		});

		panel.addButton(btnSubmit);
		panel.addButton(btnReset);

		fieldSet.add(panel);
		localPanel.add(fieldSet);
	}

	@Override
	public void show() {
		centerPanel = (ContentPanel) Registry.get(AppView.CENTER_PANEL);
		centerPanel.removeAll();
		centerPanel.setLayout(new FitLayout());
		centerPanel.add(localPanel);
		centerPanel.layout();
	}

}